require([
  "jquery",
  "ko",
  "bootstrap",
  "jquery-select2",
  "jquery-gritter",
  "jquery-blockui",
  "jquery-slimscroll",
  "akcy"
], function($, ko) {
  var akcy = arguments[arguments.length - 1];
  akcy.ui.init();

  var pageTree = akcy.tree().options({dataQueryUrl: 'config/MtrlBaseList'}).optionsExtend({
    appId: "monitor",
    nodeClicked: function(data, e) {
      pageForm.load(data);
      e.stopPropagation();
    },
    addSame: function() {
      if (pageForm.getInstance().upperMtrlCode() == -1) {
        akcy.alert.warning("请选择一项父级菜单");
        return false;
      }
      var parentID = pageForm.getInstance().upperMtrlCode();
      pageForm.getInstance().reset();
      pageForm.getInstance().upperMtrlCode(parentID);
    },
    addChild: function() {
      if (pageForm.getInstance().upperMtrlCode() == -1) {
        akcy.alert.warning("请选择一项父级菜单");
        return false;
      }
      var parentID = pageForm.getInstance().mtrlCode();
      var parentName = pageForm.getInstance().mtrlName();
      pageForm.getInstance().reset();
      pageForm.getInstance().upperMtrlCode(parentID);
      // pageForm.getInstance().P_ParentName(parentName);
    }
  }).bind("pagetree").load();

  var pageForm = akcy.form().options({
    fields: [
      {
        field: 'mtrlName',
        value: ko.observable()
      }, {
        field: 'mtrlAlias',
        value: ko.observable()
      }, {
        field: 'mtrlCode',
        value: ko.observable()
      }, {
        field: 'mtrlTypeId',
        value: ko.observable()
      }, {
        field: 'safeStockLower',
        value: ko.observable()
      }, {
        field: 'useFlag',
        value: ko.observable()
      }, {
        field: 'safeStockUpper',
        value: ko.observable()
      }, {
        field: 'mtrlid',
        value: ko.observable("")
      }, {
        field: 'dec',
        value: ko.observable()
      }, {
        field: 'displayOrder',
        value: ko.observable()
      }, {
        field: 'upperMtrlCode',
        value: ko.observable()
      }, {
        field: 'vcfType',
        value: ko.observable()
      }, {
        field: 'crtTime',
        value: ko.observable()
      }, {
        field: 'mnttime',
        value: ko.observable()
      }, {
        field: 'isDelete',
        value: ko.observable(0)
      }, {
        field: 'tankIdt',
        value: ko.observable(1)
      }, {
        field: 'reasonStockLower',
        value: ko.observable()
      }, {
        field: 'reasonStockUpper',
        value: ko.observable()
      }
    ],
    dataAddUrl: 'config/AddOrModify',
    dataUpdateUrl: 'config/AddOrModify',
    dataDeleteUrl: 'config/Delete'

  }).optionsExtend({
    appId: 'monitor',
    keyId: 'mtrlid',
    beforeAdd: function() {
      if (pageForm.getInstance().upperMtrlCode() == '') {
        akcy.alert.warning("请选择一项父级菜单");
        return false;
      }
    },
    added: function() {
      pageTree.load();
      akcy.alert.success("新增成功！");
    },
    updated: function() {
      pageTree.load();
      akcy.alert.success("保存成功！");
    },
    deleted: function(data, rst) {
      if (rst.status) {
        pageTree.load();
        pageForm.getInstance().reset();
        akcy.alert.success("删除成功！");
      } else {

        akcy.alert.error(rst.msg)
      }
    }
  }).bind("pageinfo");

});
